<template>
    <el-dialog
        :title="'添加'+title"
        :visible.sync="show"
        width="695px"
        custom-class="dialog-form"
    >
        <el-form
            ref="dialogForm"
            :model="form"
            :rules="rules"
            label-width="80px"
            class="dialog-form"
            label-position="top"
        >
            <el-form-item
                label="用户名"
                prop="nick"
            >
                <el-input
                    placeholder="请输入用户名"
                    size="small"
                    v-model="form.nick"
                    autocomplete="off"
                />
            </el-form-item>
            <el-form-item
                :label="title"
                prop="content"
            >
                <el-input
                    :placeholder="'请输入'+title+'内容'"
                    size="small"
                    v-model="form.content"
                    autocomplete="off"
                />
            </el-form-item>
            <el-form-item
                label="创建时间"
                prop="create_time"
            >
                <el-date-picker
                    style="width: 100%;"
                    size="small"
                    v-model="form.create_time"
                    type="datetime"
                    placeholder="选择日期时间"
                    format="yyyy-MM-dd HH:mm"
                />
            </el-form-item>
        </el-form>
        <div
            slot="footer"
            class="dialog-footer"
        >
            <el-button
                @click="fnCancel"
                size="small"
            >
                取 消
            </el-button>
            <el-button
                type="primary"
                size="small"
                @click="fnSubmit"
            >
                确 定
            </el-button>
        </div>
    </el-dialog>
</template>

<script>
export default {
    components: {},
    props: {
        title: {
            type: String,
            default: '添加'
        },
    },
    data() {
        return {
            show: false,
            form: {
                content: '',
                nick: '',
                create_time: '',
            },
            rules: {
                content: [
                    { required: true, message: '请输入内容', trigger: 'blur' },
                ],
                nick: [
                    { required: true, message: '请输入昵称', trigger: 'blur' },
                ],
                create_time: [
                    { required: true, message: '请选择时间', trigger: 'blur' },
                ],
            },
            resolve: null,
            reject: null,
        };
    },
    methods: {
        fnShow(){
            this.form.content = '';
            this.form.nick = '';
            this.form.create_time = new Date();
            this.show = true;
            return new Promise((resolve, reject) => {
                this.resolve = resolve;
                this.reject = reject;
            });
        },
        fnSubmit(){
            this.$refs.dialogForm.validate(valid => {
                if(valid){
                    this.show = false;
                    this.form.create_time.setSeconds(Math.round(Math.random()*60));
                    if(this.resolve){
                        this.resolve(this.form);
                    }
                }
            });
        },
        fnCancel(){
            this.show = false;
            if(this.reject){
                this.reject();
            }
        },
    }
};

</script>

<style lang="less">
    .dialog-form{
        .el-form-item{
            margin-bottom: 15px;
        }
    }
    .dialog-form{
        .el-dialog__body{
            padding: 10px 40px 0;
        }
        .el-form-item__label{
            line-height: 20px;
        }
        .dialog-footer{
            text-align: center;
        }
    }
</style>